//创建路由对象
//引用路由器对象
const express=require('express');
//创建路由器对象
const r=express.Router();
//引入pool文件
const pool=require('../pool.js');

//求职者部分
//1.求职者首页轮播图接口(post  /lunbo)
//接口地址：http://127.0.0.1:8081/v1/emps/lunbo
r.post('/lunbo',(req,res,next)=>{
   
    var obj=req.body;
    console.log(obj);


   
    var i=400;
    for(var k in obj){
        i++;
        if(obj[k]==''){
            res.send({code:i,msg:k+'不能为空'});
            return;
        }
    }  
   
    //执行sql命令
    pool.query('insert into e_lunbo set?',[obj],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        console.log(result);
        res.send({code:200,msg:'轮播图信息添加成功成功'});
    });

   
});


//2.求职者详细信息接口
r.post('/detail',(req,res,next)=>{
    var obj=req.body;
    console.log(obj);

    var i=400;
    for(var k in obj){
        i++;
        if(obj[k]==''){
            res.send({code:i,msg:k+'不能为空'});
            return;
        }
    }
    //验证身份证（没写）
   
    //执行sql命令
    pool.query('insert into emp_registe_info set?',[obj],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        console.log(result);
        res.send({code:200,msg:'信息添加成功'})
    });
   
});


//3.删除求职者详细信息
r.delete('/delete/:e_id',(req,res,next)=>{ 

    pool.query('delete  from emp_registe_info where e_id=?',[req.params.e_id],(err,result)=>{
        console.log(result);
        console.log(req.params.uid)
        if(err){
        next(err);
        return;
    }
    console.log(result)
         if(result.affectedRows===0){
	  res.send({code:501,msg:'该信息不存在，删除失败'});
	}else{
	  res.send({code:200,msg:'删除成功',data:result});
	}
        
    });
});


//4.更改求职者详细信息
r.put('/info',(req,res,next)=>{
    var obj=req.body;
    console.log(req.body);
var i=400;
for(var k in obj){
    i++;
    if(obj[k]==''){
        res.send({code:i,msg:k+'不能为空'});
        return;
    }
}
    pool.query('update emp_registe_info set? where e_id=?',[obj,obj.e_id],(err,result)=>{
        console.log(result);
        if(err){
            next(err);
            return;
        }
        if(result.affectedRows===0){
            res.send({code:501,msg:'修改失败'});
          }else{
            res.send({code:200,msg:'修改成功',data:result});
          }
        });

});


//5.求职者超级网申表接口
r.post('/sup',(req,res,next)=>{
    var obj=req.body;
    console.log(obj);

    var i=400;
    for(var k in obj){
        i++;
        if(obj[k]==''){
            res.send({code:i,msg:k+'不能为空'});
            return;
        }
    }  
    //执行sql命令
    pool.query('insert into e_sup set?',[obj],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        console.log(result);
        res.send({code:200,msg:'网申表添加成功'})
    });
   
});


//6.删除求职者超级网申表接口
r.delete('/deletesup/:sup_id',(req,res,next)=>{ 

    pool.query('delete  from e_sup where sup_id=?',[req.params.sup_id],(err,result)=>{
        console.log(result);
        console.log(req.params.uid)
        if(err){
        next(err);
        return;
    }
    console.log(result)
         if(result.affectedRows===0){
	  res.send({code:501,msg:'该信息不存在，删除失败'});
	}else{
	  res.send({code:200,msg:'删除成功',data:result});
	}
        
    });
});


//7.更新求职者超级网申表接口
r.put('/infosup',(req,res,next)=>{
    var obj=req.body;
    console.log(req.body);
var i=400;
for(var k in obj){
    i++;
    if(obj[k]==''){
        res.send({code:i,msg:k+'不能为空'});
        return;
    }
}
    pool.query('update e_sup set? where sup_id=?',[obj,obj.sup_id],(err,result)=>{
        console.log(result);
        if(err){
            next(err);
            return;
        }
        if(result.affectedRows===0){
            res.send({code:501,msg:'修改失败'});
          }else{
            res.send({code:200,msg:'修改成功',data:result});
          }
        });

});


//8.投递记录接口
r.post('/delivery',(req,res,next)=>{
    var obj=req.body;
    console.log(obj);

    var i=400;
    for(var k in obj){
        i++;
        if(obj[k]==''){
            res.send({code:i,msg:k+'不能为空'});
            return;
        }
    }  
    //执行sql命令
    pool.query('insert into e_delivery set?',[obj],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        console.log(result);
        res.send({code:200,msg:'这是投递记录'})
    });
   
});


//9.删除投递记录接口
r.delete('/deletedelivery/:d_id',(req,res,next)=>{ 

    pool.query('delete  from e_delivery where d_id=?',[req.params.d_id],(err,result)=>{
        console.log(result);
        console.log(req.params.uid)
        if(err){
        next(err);
        return;
    }
    console.log(result)
         if(result.affectedRows===0){
	  res.send({code:501,msg:'该信息不存在，删除失败'});
	}else{
	  res.send({code:200,msg:'删除成功',data:result});
	}
        
    });
});


//10.职位一级分类接口
r.post('/first',(req,res,next)=>{
    var obj=req.body;
    console.log(obj);

    var i=400;
    for(var k in obj){
        i++;
        if(obj[k]==''){
            res.send({code:i,msg:k+'不能为空'});
            return;
        }
    }  
    pool.query('insert into opt_first set?',[obj],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        console.log(result);
        res.send({code:200,msg:'职位一级分类表'})
    });
   
});


//11.职位二级分类接口
r.post('/second',(req,res,next)=>{
    var obj=req.body;
    console.log(obj);

    var i=400;
    for(var k in obj){
        i++;
        if(obj[k]==''){
            res.send({code:i,msg:k+'不能为空'});
            return;
        }
    }  
    pool.query('insert into opt_second set?',[obj],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        console.log(result);
        res.send({code:200,msg:'职位二级分类表'})
    });
   
});


//12.具体职位详细信息接口
r.post('/optdetail',(req,res,next)=>{
    var obj=req.body;
    console.log(obj);

    var i=400;
    for(var k in obj){
        i++;
        if(obj[k]==''){
            res.send({code:i,msg:k+'不能为空'});
            return;
        }
    }  
    pool.query('insert into opt_detail set?',[obj],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        console.log(result);
        res.send({code:200,msg:'具体职位详细信息表'})
    });
   
});


//13.删除具体职位详细信息接口
r.delete('/deleteopt/:odid',(req,res,next)=>{ 

    pool.query('delete  from opt_detail where odid=?',[req.params.odid],(err,result)=>{
        console.log(result);
        console.log(req.params.uid)
        if(err){
        next(err);
        return;
    }
    console.log(result)
         if(result.affectedRows===0){
	  res.send({code:501,msg:'该信息不存在，删除失败'});
	}else{
	  res.send({code:200,msg:'删除成功',data:result});
	}
        
    });
});


//14.更改具体职位详细信息接口
r.put('/infoopt',(req,res,next)=>{
    var obj=req.body;
    console.log(req.body);
var i=400;
for(var k in obj){
    i++;
    if(obj[k]==''){
        res.send({code:i,msg:k+'不能为空'});
        return;
    }
}
    pool.query('update opt_detail set? where odid=?',[obj,obj.odid],(err,result)=>{
        console.log(result);
        if(err){
            next(err);
            return;
        }
        if(result.affectedRows===0){
            res.send({code:501,msg:'修改失败'});
          }else{
            res.send({code:200,msg:'修改成功',data:result});
          }
        });

});


//15.查询职位详细信息
r.get('/infoopt/:od_name',(req,res,next)=>{
    var obj=req.params;
    pool.query('select * from opt_detail where od_name=?',[obj.od_name],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        if(result.length===0){
            res.send({code:500,msg:'查找失败'});
          }else{
            res.send({code:200,msg:'查找成功',data:result});
    }
});
});


//16.查询职位一级接口
r.get('/searchone/:of_zhilei',(req,res,next)=>{
    var obj=req.params;
    pool.query('select * from opt_first where of_zhilei=?',[obj.of_zhilei],(err,result)=>{
        console.log(result)
        if(err){
            next(err);
            return;
        }
        if(result.length===0){
            res.send({code:500,msg:'查找失败'});
          }else{
            res.send({code:200,msg:'查找成功',data:result});
    }
});
});



//17.查询职位二级接口
r.get('/searchtwo/:os_zhilei',(req,res,next)=>{
    var obj=req.params;
    pool.query('select * from opt_second where os_zhilei=?',[obj.os_zhilei],(err,result)=>{
        if(err){
            next(err);
            return;
        }
        if(result.length===0){
            res.send({code:500,msg:'查找失败'});
          }else{
            res.send({code:200,msg:'查找成功',data:result});
    }
});
});


//暴露路由对象
module.exports=r;